home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / tc2cs.arc / PSPADDR3.C < prev    next >
Text File  |  1987-06-12  |  6KB  |  174 lines

  1. /* PSPADDR3     author/revised by    Jim Deming    6-11-87     Terripin Station 
  2.     - prints the addresses of all the PSP's currently being used in memory and
  3.         the handles opened in each of those processes.
  4.         Handy if you have a lot of parent and child processes loaded.
  5.     - was compiled using Turbo C ver. 1.00
  6.     - was converted from Lattice version.  Turbo C allows you to access the
  7.          segment registers as Keywords, i.e. no definition of _DS.  For a list
  8.          of all Keywords see Page 199 Turbo C User's Guide.
  9.         Cprintf does not work the same as Lattice.  Had to add <CR>.
  10.         Extern definitions are different.
  11.         TurboC has no rstdta().
  12.  
  13.     - ASSUMES your environment area does not exceed 1000 bytes.  If it does you
  14.         need to change the array, environment, and the value in movedata().
  15.         DOS has a theoretical maximum of 32K for its environment.
  16.         - chgd for DOS 3.3, ps.environ is not null in command.com, instead after
  17.         the double null is the name of the command processor i.e. COMMAND.COM
  18.   */
  19.  
  20. struct PROG_S_PRE {                /* Program Segment Prefix */
  21.  unsigned int interrpt;    /* first two bytes are the instruction int 20h */
  22.                     /* this is a carryover from CP/M when you could */
  23.                     /* jump to the first byte of your program to exit */
  24.  unsigned int machine;    /* Memory size in paragraphs (16-bytes blocks) */
  25.  char dos1;
  26.  char call_dis[5];        /* FAR CALL to MS-DOS function dispatcher */
  27.  unsigned long Int22;    /* previous INT 22h, terminate process address IP,CS */
  28.  unsigned long Int23;    /* previous INT 23h, CNTRL-C exit address IP,CS */
  29.  unsigned long Int24;    /* previous INT 24h, Critical Error address IP,CS */
  30.  unsigned int pre_psp;    /* previous PSP segment */
  31.  unsigned char hndl[20];    /* file handles - FF means the handle not assigned */
  32.  unsigned int environ;    /* segment address of the environment block */
  33.  char dos2[4];
  34.  unsigned int num_hdl;    /* number of dos handles allocated */
  35.  unsigned int hndl_off;    /* offset  of where the file handles are located */
  36.  unsigned int hndl_seg;    /* segment of where the file handles are located */ 
  37.  char dos3[24];
  38.  char dispatch[12];        /* code to call MS-DOS dispatcher INT 21 instructions */
  39.  char fcb1[16];        /* unopened File Control Block #1 */
  40.  char fcb2[16];        /* unopened File Control Block #2 */
  41.  char dos4[4];
  42.  char dta[128];        /* default Disk Transfer Area */
  43.  } ps;
  44.  
  45.  struct EXTENDED_UNOPENED_FCB {
  46.      char extd;
  47.      char res[5];
  48.      char attr;
  49.      char drive;
  50.      char file[8];
  51.      char ext[3];
  52.      unsigned int cur_blk;
  53.      unsigned int rec_sz;
  54.      unsigned long file_sz;
  55.      unsigned int date;
  56.      unsigned int time;
  57.      char res2[8];
  58.      unsigned char cur_rec;
  59.      unsigned long rel_rec;
  60.      } exfcb;
  61.  
  62.      
  63.  
  64. extern int _psp[];
  65. /*                    Lattice externs
  66. extern int _env;
  67. extern int _esize;
  68. extern int _ss;
  69.                       TurboC externs */
  70. extern char **__argv;
  71.  
  72. char environment[1000];
  73. char command_com[] = "COMMAND.COM";
  74. char *double_null();
  75.  
  76. void main()
  77. {
  78.  
  79.     unsigned int psp;
  80.     char *p;
  81.     int cmd_com=0;
  82.     int first_time = 1;
  83.  
  84. /* Lattice builds an exact image of the environment area in its data segment.
  85.    Turbo C builds an exact image of the environment strings but leaves off the 
  86.    trailing string which contains is the program name.  So I dropped the
  87.    following test.
  88.  
  89.     if( *(long *)(_env+_esize-2) != 0x00010000 ) 
  90.     {
  91.         cprintf("Your operating system does not have the name of your\r\n");
  92.         cprintf("program at the end of the environment.  You need DOS 3.1 or\r\n");
  93.         cprintf("above.  Or you are running Novell Advance Netware ver 1.02\r\n");
  94.         cprintf("which clobbers the name area.\r\n");
  95.         _exit(1);
  96.     }
  97.   */
  98.  
  99. /*    psp = _psp[1]; / * 'Lattice'  get the segment word not the offset word */
  100.     psp = _psp[0]; /* Turbo C carries only the segment since offset is 0 */
  101.  
  102. /* get current volume name and put in dta */
  103.      exfcb.extd = 0xff;    /* makes it an extended fcb */
  104.      exfcb.attr = 0x08;    /* asking for volume */
  105.      exfcb.drive = 0;    /* of current drive  */
  106.      strcpy( exfcb.file, "???????????");    /* both fields, file & ext */
  107.  
  108. /*    rstdta();    / * reset DTA to be pointing in the PSP  - a Lattice function */
  109.     setdta( 0x80, psp ); /* TurboC equivalent of rstdta() */
  110.  
  111.     bdos( 0x11, &exfcb, 0 );
  112.     
  113.     do
  114.     {
  115. /* move chucks of memory into our data segment area so that we can work with it */
  116.         movedata( psp, 0, _DS, &ps, sizeof( struct PROG_S_PRE) );
  117.         movedata( ps.environ, 0, _DS, &environment, 1000 );
  118.  
  119. /* easy way to get progname for current program */
  120.         if( first_time )
  121.         {
  122.  
  123. /* Lattice version getting full path of program name after environment strings
  124.             cprintf("I'm %s with machine memory of %dK\r\n",
  125.                 ( _env + _esize +2 ), ps.machine >> 6 );
  126.   */                
  127. /* Turbo C missing program name after environment so use argv */
  128.             cprintf("I'm %s with machine memory of %dK\r\n",
  129.                 *__argv, ps.machine >> 6 );
  130.             if( ps.dta[0] == '\377' )
  131.             {
  132.                 ps.dta[19] = '\0';
  133.                 cprintf("   current volume name is %s\r\n", &ps.dta[8]);
  134.             }
  135.             cprintf("\r\n");
  136.             first_time = 0;
  137.         }
  138.  
  139.         if( ps.pre_psp == ps.hndl_seg ) cmd_com = 1;    /* in COMMAND.COM */
  140.         if( ps.environ == 0 )        /* not DOS 3.3 */
  141.             p = command_com;
  142.         else {        
  143.         
  144. /* find the name of the program following the environment space, Page 4-3 of
  145.     MS-DOS manual, document # 8411-310-02, part # 036-014-012 */
  146.             p = environment;
  147.             while( *double_null( &p ));
  148.  
  149.                if( cmd_com ) p++;  else  p += 3;
  150.         }
  151.         cprintf("psp at %dK for %s", psp>>6, p );
  152.         cprintf(" maximum number of handles is %d\r\n",ps.num_hdl);
  153.         if( ps.hndl_off == 0x18 && ps.hndl_seg == psp )
  154.         { 
  155. /* display which handles are active for each program */
  156.         /* the field psp is being used as a work int - not good form but ...*/
  157.             for( psp = 0; psp < 20; psp++)
  158.             {
  159.                 if( ps.hndl[psp] != 0xff )
  160.                     cprintf(" %d",psp);
  161.             }
  162.             cprintf("\r\n");
  163.             psp = ps.pre_psp;
  164.         } else {
  165.             cprintf("handles not in psp ");
  166.         }
  167.     } while( ! cmd_com );
  168. }
  169. char *double_null( p )
  170. char **p;
  171. {
  172.     while( *(*p)++ );
  173.     return( *p );
  174. }